package com.wind.plugin.login;

import com.intellij.openapi.ui.Messages;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import com.intellij.ui.components.JBPasswordField;
import com.intellij.ui.components.labels.LinkLabel;
import com.intellij.ui.components.JBPanel;

import javax.swing.*;
import java.awt.*;

public class LoginDialog extends JDialog {

    private JBTextField usernameField;
    private JBPasswordField passwordField;
    private LinkLabel<Void> loginLink;
    private LinkLabel<Void> cancelLink;

    public LoginDialog(JFrame parent) {
        super(parent, "登录", true);
        initializeComponents();
        layoutComponents();
        addListeners();
        pack();
        setLocationToBottomRight(parent);
    }

    private void setLocationToBottomRight(JFrame parent) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension dialogSize = getSize();
        int x = screenSize.width - dialogSize.width - 10; // 10 is a margin from the right edge
        int y = screenSize.height - dialogSize.height - 10; // 10 is a margin from the bottom edge
        setLocation(x, y);
    }

    private void initializeComponents() {
        usernameField = new JBTextField();
        passwordField = new JBPasswordField();
        loginLink = new LinkLabel<>("登录", null);
        cancelLink = new LinkLabel<>("取消", null);
    }

    private void layoutComponents() {
        JBPanel panel = new JBPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 标题
        JBLabel titleLabel = new JBLabel("代码检查插件");
        titleLabel.setFont(new Font("Arial", Font.BOLD, 18));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(titleLabel, gbc);

        // 用户名
        JBLabel usernameLabel = new JBLabel("用户名:");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(usernameField, gbc);

        // 密码
        JBLabel passwordLabel = new JBLabel("密码:");
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(passwordField, gbc);

        // 登录和取消链接
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(loginLink, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(cancelLink, gbc);

        setContentPane(panel);
    }

    private void addListeners() {
        loginLink.setListener((source, event) -> {
            performLogin();
        }, null);

        cancelLink.setListener((source, event) -> {
            dispose();
        }, null);
    }

    private void performLogin() {
        String username = usernameField.getText();
        char[] password = passwordField.getPassword();

        // 这里可以调用验证用户凭据的方法
        if (validateCredentials(username, password)) {
            Messages.showInfoMessage("登录成功", "成功");
            dispose();
            // 存储用户会话信息
            storeSession(username);
            // 显示主界面或其他操作
            showMainInterface();
        } else {
            Messages.showInfoMessage("用户名或密码错误", "错误");
        }
    }

    private boolean validateCredentials(String username, char[] password) {
        // 这里可以调用网络请求或本地验证方法
        // 示例：假设用户名为 "admin"，密码为 "123456"
        String correctUsername = "admin";
        String correctPassword = "123456";
        return username.equals(correctUsername) && new String(password).equals(correctPassword);
    }

    private void storeSession(String username) {
        // 存储用户会话信息，例如使用 Preferences API
        java.util.prefs.Preferences prefs = java.util.prefs.Preferences.userNodeForPackage(LoginDialog.class);
        prefs.put("username", username);
    }

    private void showMainInterface() {
        // 显示主界面或其他操作
        Messages.showInfoMessage("欢迎 " + usernameField.getText() + "！", "欢迎");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            LoginDialog loginPage = new LoginDialog(frame);
            loginPage.setVisible(true);
        });
    }
}
